package com.prik.observer.weather;

import java.util.ArrayList;

/**
 * @author prik
 * @date 2019/7/17
 */
public class WeatherData implements Subject {

    private ArrayList<Observer> observers;
    private float temperature;
    private float humidity;
    private float pressure;

    public WeatherData() {
        observers = new ArrayList();
    }

    @Override
    public void registerObserver(Observer o) {
        observers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        observers.remove(o);
    }




    @Override
    public void notifyObservers() {
        observers.forEach(observer -> observer.update(temperature, humidity, pressure));
    }

    /**
     * 数据更新时执行
     */
    void measurementsChanged() {
        notifyObservers();
    }

    /**
     * 模拟测试
     *
     * @param temperature
     * @param humidity
     * @param pressure
     */
    void setMeasurements(float temperature, float humidity, float pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }
}
